{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "f4174475",
   "metadata": {},
   "source": [
    "## 本周知识点\n",
    "#### 1，字典的一般方法\n",
    "> 1. 字典的查询\n",
    ">> 1.先看key是否存在：in\n",
    "> 2. 字典的修改\n",
    "> 3. 字典的删除\n",
    "> 4. 字典的增加（dict_name[key]=value)\n",
    "### 2.字典的进阶方法\n",
    "> 1. 字典初始化\n",
    ">> 1.D.fromkets()\n",
    ">> 2.Dsetdefault()\n",
    "### 3.字典的实践1 课本\n",
    "### 4.字典的实践2 课本\n",
    "### 5.字典的实践3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "b462c5c4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'stu1801': 'ziqi', 'stu1802': 'xiaofen', 'stu1803': 'lihua'}\n"
     ]
    }
   ],
   "source": [
    "stu_info={\n",
    "    \"stu1801\":\"ziqi\",\n",
    "    \"stu1802\":\"xiaofen\",\n",
    "    \"stu1803\":\"lihua\",\n",
    "}\n",
    "print(stu_info)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6d8c9f8a",
   "metadata": {},
   "source": [
    "1. 字典的查询"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "af40ad87",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'ziqi'"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stu_info[\"stu1801\"]#直接查询，当关键词不存在有可能报错"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "64260b31",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function get:\n",
      "\n",
      "get(key, default=None, /) method of builtins.dict instance\n",
      "    Return the value for key if key is in the dictionary, else default.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(stu_info.get)#用get方法查找，当关键词不存在不会报错"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "f64bc9fb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'ziqi'"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stu_info.get(\"stu1801\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "7c12a3f6",
   "metadata": {},
   "outputs": [],
   "source": [
    "stu_info.get(\"stu1804\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "c79e766d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "None\n"
     ]
    }
   ],
   "source": [
    "print(stu_info.get(\"stu1804\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "636f190c",
   "metadata": {},
   "source": [
    "> 2. 修改，新增\n",
    ">> 1.先查询key是否存在。使用in\n",
    ">> 2.如果key存在，修改：dict_name[]=value\n",
    ">> 3.如果不存在 新增：dict_name[]=value"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "b7c122b2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查询stu_1804是否存在\n",
    "\"stu1804\" in stu_info"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "45cc8d51",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stu1801': 'ziqi', 'stu1802': 'xiaofen', 'stu1803': 'liuyu'}"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "if \"stu1803\" in stu_info:\n",
    "     stu_info[\"stu1803\"]=\"liuyu\"#lihua修改变成liuyu\n",
    "stu_info\n",
    "    "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5d8dfb6d",
   "metadata": {},
   "source": [
    "> 3.字典的删除\n",
    ">> 1. D.pop()的一般操作\n",
    ">>> 1.先查询是否存在 in\n",
    ">>> 2.在使用删除方法\n",
    ">> 2. D.popitem"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "8411b7fe",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function pop:\n",
      "\n",
      "pop(...) method of builtins.dict instance\n",
      "    D.pop(k[,d]) -> v, remove specified key and return the corresponding value.\n",
      "    If key is not found, d is returned if given, otherwise KeyError is raised\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(stu_info.pop)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "fb8dc126",
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "pop expected at least 1 argument, got 0",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-22-291a890be2a8>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mstu_info\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mpop\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;31m#不正确，不同列表删除最后一个\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m: pop expected at least 1 argument, got 0"
     ]
    }
   ],
   "source": [
    "stu_info.pop()#不正确，不同列表删除最后一个"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "d4cfc2e0",
   "metadata": {},
   "outputs": [],
   "source": [
    "#删除正确用法\n",
    "if \"stu1803\" in stu_info:\n",
    "    stu_info.pop(\"stu1803\")\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "7ebfad66",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stu1801': 'ziqi', 'stu1802': 'xiaofen'}"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stu_info"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "5d3ee729",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function popitem:\n",
      "\n",
      "popitem() method of builtins.dict instance\n",
      "    Remove and return a (key, value) pair as a 2-tuple.\n",
      "    \n",
      "    Pairs are returned in LIFO (last-in, first-out) order.\n",
      "    Raises KeyError if the dict is empty.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(stu_info.popitem)#查询方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "b233406b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('stu1803', 'lihua')"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stu_info.popitem()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "17371700",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stu1801': 'ziqi', 'stu1802': 'xiaofen'}"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stu_info"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "834a198f",
   "metadata": {},
   "outputs": [
    {
     "ename": "KeyboardInterrupt",
     "evalue": "Interrupted by user",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyboardInterrupt\u001b[0m                         Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-44-0791f79466e6>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      4\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      5\u001b[0m \u001b[0mvowels\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m[\u001b[0m\u001b[1;34m\"a\"\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m\"e\"\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m\"i\"\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m\"o\"\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m\"u\"\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 6\u001b[1;33m \u001b[0mword\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0minput\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"Provide a word to search for vowels:\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      7\u001b[0m \u001b[0mfound\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m{\u001b[0m\u001b[1;33m}\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      8\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mD:\\anaconda\\lib\\site-packages\\ipykernel\\kernelbase.py\u001b[0m in \u001b[0;36mraw_input\u001b[1;34m(self, prompt)\u001b[0m\n\u001b[0;32m    858\u001b[0m                 \u001b[1;34m\"raw_input was called, but this frontend does not support input requests.\"\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    859\u001b[0m             )\n\u001b[1;32m--> 860\u001b[1;33m         return self._input_request(str(prompt),\n\u001b[0m\u001b[0;32m    861\u001b[0m             \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_parent_ident\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    862\u001b[0m             \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_parent_header\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mD:\\anaconda\\lib\\site-packages\\ipykernel\\kernelbase.py\u001b[0m in \u001b[0;36m_input_request\u001b[1;34m(self, prompt, ident, parent, password)\u001b[0m\n\u001b[0;32m    902\u001b[0m             \u001b[1;32mexcept\u001b[0m \u001b[0mKeyboardInterrupt\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    903\u001b[0m                 \u001b[1;31m# re-raise KeyboardInterrupt, to truncate traceback\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 904\u001b[1;33m                 \u001b[1;32mraise\u001b[0m \u001b[0mKeyboardInterrupt\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"Interrupted by user\"\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;32mfrom\u001b[0m \u001b[1;32mNone\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    905\u001b[0m             \u001b[1;32mexcept\u001b[0m \u001b[0mException\u001b[0m \u001b[1;32mas\u001b[0m \u001b[0me\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    906\u001b[0m                 \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mlog\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mwarning\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"Invalid Message:\"\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mexc_info\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;32mTrue\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mKeyboardInterrupt\u001b[0m: Interrupted by user"
     ]
    }
   ],
   "source": [
    "## 字典实践1\n",
    "# -*- coding:utf-8 -*-\n",
    "# Author:Zhichao\n",
    " \n",
    "vowels = [\"a\",\"e\",\"i\",\"o\",\"u\"]\n",
    "word = input(\"Provide a word to search for vowels:\")\n",
    "found = {}\n",
    " \n",
    " # found[\"a\"]=0\n",
    " # found[\"e\"]=0\n",
    " # found[\"i\"]=0\n",
    " # found[\"o\"]=0\n",
    " # found[\"u\"]=0\n",
    "found = found.fromkeys(vowels,0)\n",
    " \n",
    "for letter in word:\n",
    "     if letter in vowels:\n",
    "        found[letter] += 1\n",
    " \n",
    " # print(found.items())\n",
    "for k,v in found.items():\n",
    "     print(k,\"was found\",v,\"time(s)\")\n",
    " # print(found)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e0dbd800",
   "metadata": {},
   "outputs": [],
   "source": [
    "#目标：查询元音字母是否出现在word文本中。并且统计出现次数\n",
    "vowels = [\"a\",\"e\",\"i\",\"o\",\"u\"]\n",
    "word = input(\"Provide a word to search for vowels:\")\n",
    "found = {}\n",
    "\n",
    "# 字典 初始化 的值\n",
    "# found[\"a\"]=0\n",
    "# found[\"e\"]=0\n",
    "# found[\"i\"]=0\n",
    "# found[\"o\"]=0\n",
    "# found[\"u\"]=0\n",
    "\n",
    "for i in vowels:\n",
    "    #　使用字典的新增　构建初始化字典\n",
    "    found[i] = 0 \n",
    "    \n",
    "# 循环用户输入的文本 ，出现一次元音字符，对应的元音字符+1 \n",
    "for i in word:\n",
    "    if i in vowels:\n",
    "        found[i] +=1 \n",
    "found"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3118614b",
   "metadata": {},
   "source": [
    "> 字典的进阶方法：初始化所有值fromkeys（）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "29768cd5",
   "metadata": {},
   "outputs": [],
   "source": [
    "help(found.fromkeys)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "a9a798a4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Provide a word to search for vowels:ashnufau\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'a': 2, 'e': 0, 'i': 0, 'o': 0, 'u': 2}"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "vowels = [\"a\",\"e\",\"i\",\"o\",\"u\"]\n",
    "word = input(\"Provide a word to search for vowels:\")\n",
    "found = {}\n",
    "\n",
    "found=found.fromkeys(vowels,0)\n",
    "for i in word:\n",
    "    if i in vowels:\n",
    "        found[i] +=1 \n",
    "found"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "d8fbe4f5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a': 2, 'e': 0, 'i': 0, 'o': 0, 'u': 2}"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "found"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1bedc16b",
   "metadata": {},
   "source": [
    "> 字典进阶：初始化特定的值 setdefault()\n",
    ">> 1.查询key是否在字典中\n",
    ">>> 1.出现过，就当没看见，不做任何操作\n",
    ">>> 2.key没出现，给指定key和value（指定初始化值）字典中不存在该元素时，初始化其值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "b99e7a6e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function setdefault:\n",
      "\n",
      "setdefault(key, default=None, /) method of builtins.dict instance\n",
      "    Insert key with a value of default if key is not in the dictionary.\n",
      "    \n",
      "    Return the value for key if key is in the dictionary, else default.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(found.setdefault)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "8a71cfaf",
   "metadata": {},
   "outputs": [],
   "source": [
    "stu_password={\n",
    "    \"xiaoming\":\"222333\"\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "e13948b7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'123456'"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stu_password.setdefault(\"lihua\",\"123456\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "663e3aca",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'xiaoming': '222333', 'lihua': '123456'}"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stu_password"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "66851182",
   "metadata": {},
   "source": [
    "## 字典的实践2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "690cddf9",
   "metadata": {},
   "outputs": [],
   "source": [
    "#优化实践1"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
